package com.kcsoft.welcome.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kcsoft.welcome.mapper.MemberMapper;
import com.kcsoft.welcome.pojo.Member;
import com.kcsoft.welcome.pojo.PageBean;
import com.kcsoft.welcome.pojo.Result;
import com.kcsoft.welcome.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Result add(Member member) {
        memberMapper.add(member);
        return Result.success();
    }

    @Override
    public PageBean<Member> list(Integer pageNum, Integer pageSize) {
        PageBean<Member> pb = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        List<Member> list = memberMapper.list();

        Page<Member> m = (Page<Member>) list;

        pb.setTotal(m.getTotal());

        pb.setItems(m.getResult());

        return pb;
    }

    @Override
    public void update(Member member) {
        memberMapper.update(member);
    }

    @Override
    public void delete(Integer id) {
        memberMapper.delete(id);
    }

}
